package com.sjlh.centurylianhua.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.User;
import com.sjlh.centurylianhua.mapper.UserMapper;
import com.sjlh.centurylianhua.utils.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Resource
    private UserMapper userMapper;
    /**
     * 分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isnotNull(searchCon.getName()), "userName", searchCon.getName());
        wrapper.like(StringUtils.isnotNull(searchCon.getName2()),"sex",searchCon.getName2());
        wrapper.eq(searchCon.getTypeName()!=null,"isLockout",searchCon.getTypeName());
        wrapper.ge(StringUtils.isnotNullObj(searchCon.getBeginDate()),"beiyong2",searchCon.getBeginDate());
        wrapper.le(StringUtils.isnotNullObj(searchCon.getEndDate()),"beiyong2",searchCon.getEndDate());
        // 封装分页对象
        Page<User> page = new Page<User>(searchCon.getPage(), searchCon.getLimit());
        Page<User> userPage = userMapper.selectPage(page, wrapper);
        List<User> list=userPage.getRecords();//所有数据
        long count=userPage.getTotal();//总条数
        ResultData rs=new ResultData(true,20000,"成功",list,count);
        return rs;
    }
    /**
     *根据员工名字精确查询员工
     * */
    public User getOne(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq( "userName", user.getUserName());
        return userMapper.selectOne(wrapper);
    }

    /**
     *
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody User user){
        User d = this.getOne(user);
        if (d == null) {
            int r=userMapper.insert(user);
            if(r==1)
                return new ResultData(true,20000,"添加员工成功",true,null);
            else
                return new ResultData(false,20000,"添加员工失败",false,null);
        } else {
            return new ResultData(false,20001,"员工名已存在",false,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody User user){
            int r = userMapper.updateById(user);
            if(r==1)
                return new ResultData(true,20000,"修改员工成功",true,null);
            else
                return new ResultData(false,20000,"修改员工失败",false,null);


    }
    @RequestMapping("/setPassword")
    public Object setPassword(@RequestBody SearchCon searchCon){

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id",searchCon.getTypeId());
        User one = userMapper.selectOne(userQueryWrapper);
        System.out.println(searchCon);
        System.out.println(one);
        if (one!=null){//判断用户是否存在
            if(one.getPassword().equals(searchCon.getName())){//判断旧密码是否正确正确则修改
                UpdateWrapper<User> wrapper = new UpdateWrapper<User>();
                wrapper.set("password",searchCon.getAddress());
                wrapper.eq( "id", searchCon.getTypeId());
                int update = userMapper.update(null, wrapper);
                return new ResultData(false,20000,"修改成功",true,null);
            }else {//不正确返回提示
                return new ResultData(false,20000,"原密码错误",false,null);
            }
        }else {
            return new ResultData(false,20000,"登录信息过期，请重新登录",false,null);
        }
    }
    @RequestMapping("/resetPassword")
    public Object resetPassword(@RequestBody User user){
        UpdateWrapper<User> wrapper = new UpdateWrapper<User>();
        wrapper.set("password","123456");
        wrapper.eq( "id", user.getId());
        int update = userMapper.update(null, wrapper);
        return new ResultData(false,20000,"密码重置为123456",true,null);
    }

    /**
     *
     * */
    @RequestMapping("/del")
    public Object del(@RequestBody User user){
        int rs = userMapper.deleteById(user.getId());
        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }

    /**
     * 用户登录接口
     *
     */
    @PostMapping("/login")
    public Object login(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("account", user.getAccount());
        User u = userMapper.selectOne(wrapper);
        if (u == null) {
            return new ResultData(false,20001,"用户不存在",null,null);
        } else {
            if(u.getIsLockout().equals(0)){
                return new ResultData(false,20001,"用户被锁定",null,null);
            }else {
                if (u.getPassword().equals(user.getPassword())) {
                    user.setId(u.getId());
                    user.setBeiyong3(u.getBeiyong3());
                    String token = JwtUtils.getToken(user.getAccount());
                    String account = JwtUtils.parseToken(token).getSubject();
                    UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("pwdWrong", 0).eq("id", user.getId());
                    userMapper.update(user, updateWrapper);
                    Map<String,Object> map = new HashMap<>();
                    map.put("token",token);
                    map.put("user",user);
                    return new ResultData(true,20000,"登陆成功",map,null);
                } else {
                    UpdateWrapper<User> updateWapper = new UpdateWrapper<>();
                    if(u.getPwdWrong() == null || u.getPwdWrong() < 4) {
                        updateWapper.set("pwdWrong", u.getPwdWrong() == null ? 1 : u.getPwdWrong() + 1).eq("id", u.getId());
                    }else {
                        updateWapper.set("isLockout", "0").set("pwdWrong", u.getPwdWrong() == null ? 1 : u.getPwdWrong() + 1).set("lockTime", new Date()).eq("id", u.getId());
                    }
                    userMapper.update(u,updateWapper);
                    return new ResultData(false,20001,"密码不正确",null,null);
                }
            }



        }

    }

    @GetMapping("/info")  // "token:xxx"
    public ResultData info(String token){
        String account = JwtUtils.parseToken(token).getSubject();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        User user = userMapper.selectOne(wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return new ResultData(true,20000,"登陆成功",map,null);
    }

    @PostMapping("/logout")  // "token:xxx"
    public ResultData logout(){
        return new ResultData(true,20000,"退出成功",null,null);
    }
}
